package com.example.hzz.geoquize;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by huangzezhan on 2018/8/1.
 */

public class CheatActivity extends Activity {
    public static final String TAG = "CheatActivity";
    private static final String CHEATER = "cheater";
    private static final String ANSWER = "answer";
    private Button show;
    private TextView showText;
    private boolean isTrueAnswer = false;
    private boolean mIsCheater = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);
        if(savedInstanceState!=null){
            isTrueAnswer = savedInstanceState.getBoolean(ANSWER);
            mIsCheater = savedInstanceState.getBoolean(CHEATER);
            setActivityResult();
        }
        showText = (TextView) findViewById(R.id.answerTextView);
        show = (Button) findViewById(R.id.showAnswerButton);
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIsCheater = true;
                Log.i(TAG, "isTrueAnswer:" + isTrueAnswer);
                showText.setText("the answer is " + isTrueAnswer);
                setActivityResult();
            }
        });
        isTrueAnswer = getIntent().getBooleanExtra("answer", false);
    }

    private void setActivityResult(){
        Intent i = new Intent();
        i.putExtra("Answer", isTrueAnswer);
        i.putExtra("isCheat",true);
        setResult(RESULT_OK, i);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(CHEATER,mIsCheater);
        outState.putBoolean(ANSWER,isTrueAnswer);
    }
}
